﻿package  
{
	import com.event.EventZheng;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import com.utils.GetMovieClip;
	
	/**
	 * 显示读取出来的图片
	 * @author zhengzihua
	 */
	public class ShowLoader extends Sprite
	{
		//public static var currentUrl:String = "default";//当前显示的url
		private var contain:Sprite = new Sprite();
		private var loader:Loader = new Loader();
		private var textField:TextField = new TextField();
		private var txt:String;
		private var progressMc:MovieClip;
		public function ShowLoader() 
		{
			addChild(contain);
			addChild(textField);
			progressMc=GetMovieClip.getMovieClip("ProgressMc");
		}
		public function showPic(url:String, txt:String):void {
			this.txt = txt;
			addChild(progressMc);
			progressMc.x = 130;
			progressMc.y = 200;
			progressMc.gotoAndPlay("startLoad");
			loader.load(new URLRequest(url));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplement);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
			loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
			contain.addChild(loader);
		}
		
		private function onStatus(e:HTTPStatusEvent):void 
		{
		}
		
		private function onLoading(e:ProgressEvent):void 
		{
			var num:int = 100 * e.bytesLoaded / e.bytesTotal;
			progressMc.bar.scaleX = num / 100;
		}
		
		private function onComplement(e:Event):void 
		{
			textField.text = txt;
			textField.autoSize = TextFieldAutoSize.LEFT;//左对齐
			textField.selectable = false;
			textField.textColor = 0x4473924;
			textField.width = loader.width;
			textField.y = loader.height;
			textField.wordWrap = true;
			textField.setTextFormat(new TextFormat(null, 14));
			progressMc.gotoAndPlay("endLoad");
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplement);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
			loader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
			
			loader.content.alpha = 0;
			TweenMax.to(loader.content, 2, {alpha:1});
			//图片读取完成,抛出事件
			var event:EventZheng = new EventZheng(StaticInfo.JPGLOADERED);
			event.obj.p = new Point(textField.x, textField.y);
			dispatchEvent(event);
		}
		
	}
	
}